package com.itheima.web;

import com.itheima.utils.FileUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController

public class UploadController {


    //redis
    @Resource
    private RedisTemplate<String,Object> redisTemplate;



    /**
     * 文件上传
     */
    @RequestMapping("/upload")
    public String upload(@RequestParam("myfile") MultipartFile multipartFile) throws IOException {

        String path = FileUtils.upload(multipartFile.getOriginalFilename(), multipartFile.getInputStream());

        //将path存放到file：allset
        redisTemplate.opsForSet().add("file:allset",path);
        //将file：{path} 设置有限时间
        redisTemplate.opsForValue().set("file:"+path,1,30, TimeUnit.SECONDS);

        return path;
    }
}
